#include "sync_code.h"

using namespace VLRTSP;


/**
 * @Author: CimZzz
 * @Date: 2022-01-13 11:23:56
 * 
 * 构造函数
 * @param min 最小值，默认为 0
 * @param max 最大值，默认为 UINT32_MAX
 */
SyncCode::SyncCode(uint32_t min, uint32_t max) {
	this->code = min;
	this->min = min;
	this->max = max;
}

/**
 * @Author: CimZzz
 * @Date: 2022-01-13 11:24:25
 * 
 * 析构函数
 */
SyncCode::~SyncCode() {

}

/**
 * @Author: CimZzz
 * @Date: 2022-01-13 11:25:24
 * 
 * 检查是否同步
 * @param code 记录的同步码
 * @return 如果记录的同步码与当前同步码相同，返回 ture
 */
bool SyncCode::checkSync(uint32_t code) {
	std::lock_guard<std::mutex> lock(this->mutex);
	return this->code == code;
}

/**
 * @Author: CimZzz
 * @Date: 2022-01-13 15:46:03
 * 
 * 获取当前同步码
 * @return 返回当前同步码
 */
uint32_t SyncCode::getCode() {
	return this->code;
}

/**
 * @Author: CimZzz
 * @Date: 2022-01-13 11:25:37
 * 
 * 变更同步码
 * @return 返回变化后的同步码
 */
uint32_t SyncCode::nextCode() {
	std::lock_guard<std::mutex> lock(this->mutex);
	if(this->code >= this->max) {
		this->code = this->min;
	} else {
		this->code ++;
	}
	return this->code;
}